/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Swing;

import common.CarUtils;
import entities.Car;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

/**
 *
 * @author A
 */
public class CarScreen {
    Object[][] rows;

        
    public void showDialogForUser(String textToShow){
        JOptionPane.showMessageDialog(null, textToShow);
    }
    
 public void populateCarTable(JTable CarTable, JPanel CarPanel, 
         final JTextField CarOptTxtField){
     String[] columns = { "ID", "Marka", "Typ", "Rok produkcji", "Imię", "Nazwisko"};
     ArrayList<Car> carsData = new CarUtils().getCarsData();
     rows = new Object[carsData.size()][columns.length];
     for(int i = 0 ; i < carsData.size() ; i++){
          rows[i][0] = carsData.get(i).getCarId();
          rows[i][1] = carsData.get(i).getMark();
          rows[i][2] = carsData.get(i).getType();
          rows[i][3] = carsData.get(i).getYearOfProduction();
          rows[i][4] = carsData.get(i).getCustomer().getFirstName();
          rows[i][5] = carsData.get(i).getCustomer().getLastName();
     }
     TableModel carModel = new DefaultTableModel(rows, columns);
     CarTable.setModel(carModel); 
     final TableRowSorter<TableModel> sorter;
     sorter = new TableRowSorter<>(carModel);
     CarTable.setRowSorter(sorter);
     CarPanel.repaint();
     
     if(CarOptTxtField != null){
         CarOptTxtField.getDocument().addDocumentListener(new DocumentListener() {

             @Override
             public void insertUpdate(DocumentEvent e) {
                String expr = CarOptTxtField.getText();
                sorter.setRowFilter(RowFilter.regexFilter(expr));
                sorter.setSortKeys(null);
             }

             @Override
             public void removeUpdate(DocumentEvent e) {
                String expr = CarOptTxtField.getText();
                sorter.setRowFilter(RowFilter.regexFilter(expr));
                sorter.setSortKeys(null);
             }

             @Override
             public void changedUpdate(DocumentEvent e) {
                String expr = CarOptTxtField.getText();
                sorter.setRowFilter(RowFilter.regexFilter(expr));
                sorter.setSortKeys(null);
             }
             
         });
     }   
 }    
 
      public void editCarData(JFrame frame, final JTabbedPane MainTabbedPane, final int idCar){
        if(idCar > 0){
           CarsDialog carsDialog = new CarsDialog(frame, idCar, true);
           carsDialog.setVisible(true);
        }
      }
      
      public void deleteCarData(int carId){
          CarUtils carUtils = new CarUtils();
          carUtils.deleteCarFromDatabase(carId);
      }
}
